import { getInfo, getUserDetailById } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
// import store from '@/store'
import router from '@/router'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updataToken(state, value) {
      state.token = value
      setToken(value)
    },
    delToken(state, value) {
      state.token = ''
      removeToken()
    },
    changeUserInfo(state, value) {
      state.userInfo = value
    },
    loginOut(state, value) {
      state.token = ''
      state.userInfo = ''
      // console.log(value)
      router.push('/login?return_url=' + value)
    }
  },
  actions: {
    async getUserInfo(context) {
      const res1 = await getInfo()
      const res2 = await getUserDetailById(res1.data.userId)
      // console.log('用来获取用户信息的,', res)
      context.commit('changeUserInfo', { ...res1.data, ...res2.data })
    }
  }
}
